import 'package:flutter/material.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';
import '../utils/adapt.dart';
import '../utils/request.dart';
import 'package:provider/provider.dart';
import '../provider/playAudioList.dart';

class BannerList extends StatefulWidget {
  BannerList({Key? key}) : super(key: key);

  @override
  _BannerListState createState() => _BannerListState();
}

class _BannerListState extends State<BannerList> {
  List _list = [];
  void _getData() async {
    var res = await getHttp('/personalized/newsong?limit=12');
    var arr = res['result'];
    var arr2 = arr
        .map((item) =>
            {"name": item['name'], "picUrl": item['picUrl'], "id": item['id']})
        .toList();
    for (int i = 0; i < 3; i++) {
      var a = arr2.getRange(i * 4, (i + 1) * 4).toList();
      _list.add(a);
    }
    this.setState(() {});
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getData();
  }

  @override
  Widget build(BuildContext context) {
    var playAudioList = Provider.of<PlayAudioList>(context);
    return Container(
      height: Adapt.px(440),
      child: _list.length == 0
          ? Text("数据加载中")
          : new Swiper(
              controller: SwiperController(),
              itemBuilder: (BuildContext context, int index) {
                return Container(
                  child: Column(
                    children: [
                      InkWell(
                        onTap: () {
                          var newList = _list[index]
                              .map((item) => {
                                    "id": item['id'],
                                    "name": item['name'],
                                    "picUrl": item['picUrl']
                                  })
                              .toList();
                          playAudioList.setCurrentIndex(0);
                          playAudioList.setAudioList(newList);
                          Navigator.pushNamed(context, "/audioplayer",
                              arguments: {});
                        },
                        child: Container(
                          height: Adapt.px(100),
                          padding: EdgeInsets.only(right: Adapt.px(30)),
                          margin: EdgeInsets.only(top: Adapt.px(10)),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              ClipRRect(
                                borderRadius:
                                    BorderRadius.circular(Adapt.px(20)),
                                child: Image.network(
                                  _list[index][0]['picUrl'] + '?param=100y100',
                                  width: Adapt.px(96),
                                  height: Adapt.px(96),
                                ),
                              ),
                              Container(
                                width: Adapt.px(360),
                                child: Text(
                                  _list[index][0]['name'],
                                  style: TextStyle(fontSize: Adapt.px(28)),
                                ),
                              ),
                              Icon(
                                Icons.play_circle_filled,
                                size: Adapt.px(36),
                              )
                            ],
                          ),
                        ),
                      ),
                      InkWell(
                        onTap: () {
                          var newList = _list[index]
                              .map((item) => {
                                    "id": item['id'],
                                    "name": item['name'],
                                    "picUrl": item['picUrl']
                                  })
                              .toList();
                          playAudioList.setCurrentIndex(1);
                          playAudioList.setAudioList(newList);
                          Navigator.pushNamed(context, "/audioplayer",
                              arguments: {});
                        },
                        child: Container(
                          height: Adapt.px(100),
                          padding: EdgeInsets.only(right: Adapt.px(30)),
                          margin: EdgeInsets.only(top: Adapt.px(10)),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              ClipRRect(
                                borderRadius:
                                    BorderRadius.circular(Adapt.px(20)),
                                child: Image.network(
                                  _list[index][1]['picUrl'] + '?param=100y100',
                                  width: Adapt.px(96),
                                  height: Adapt.px(96),
                                ),
                              ),
                              Container(
                                width: Adapt.px(360),
                                child: Text(
                                  _list[index][1]['name'],
                                  style: TextStyle(fontSize: Adapt.px(28)),
                                ),
                              ),
                              Icon(
                                Icons.play_circle_filled,
                                size: Adapt.px(36),
                              )
                            ],
                          ),
                        ),
                      ),
                      InkWell(
                        onTap: () {
                          var newList = _list[index]
                              .map((item) => {
                                    "id": item['id'],
                                    "name": item['name'],
                                    "picUrl": item['picUrl']
                                  })
                              .toList();
                          playAudioList.setCurrentIndex(2);
                          playAudioList.setAudioList(newList);
                          Navigator.pushNamed(context, "/audioplayer",
                              arguments: {});
                        },
                        child: Container(
                          height: Adapt.px(100),
                          padding: EdgeInsets.only(right: Adapt.px(30)),
                          margin: EdgeInsets.only(top: Adapt.px(10)),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              ClipRRect(
                                borderRadius:
                                    BorderRadius.circular(Adapt.px(20)),
                                child: Image.network(
                                  _list[index][2]['picUrl'] + '?param=100y100',
                                  width: Adapt.px(96),
                                  height: Adapt.px(96),
                                ),
                              ),
                              Container(
                                width: Adapt.px(360),
                                child: Text(
                                  _list[index][2]['name'],
                                  style: TextStyle(fontSize: Adapt.px(28)),
                                ),
                              ),
                              Icon(
                                Icons.play_circle_filled,
                                size: Adapt.px(36),
                              )
                            ],
                          ),
                        ),
                      ),
                      InkWell(
                        onTap: () {
                          var newList = _list[index]
                              .map((item) => {
                                    "id": item['id'],
                                    "name": item['name'],
                                    "picUrl": item['picUrl']
                                  })
                              .toList();
                          playAudioList.setCurrentIndex(3);
                          playAudioList.setAudioList(newList);
                          Navigator.pushNamed(context, "/audioplayer",
                              arguments: {});
                        },
                        child: Container(
                          height: Adapt.px(100),
                          padding: EdgeInsets.only(right: Adapt.px(30)),
                          margin: EdgeInsets.only(top: Adapt.px(10)),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              ClipRRect(
                                borderRadius:
                                    BorderRadius.circular(Adapt.px(20)),
                                child: Image.network(
                                  _list[index][3]['picUrl'] + '?param=50y50',
                                  width: Adapt.px(96),
                                  height: Adapt.px(96),
                                ),
                              ),
                              Container(
                                width: Adapt.px(360),
                                child: Text(
                                  _list[index][3]['name'],
                                  style: TextStyle(fontSize: Adapt.px(28)),
                                ),
                              ),
                              Icon(
                                Icons.play_circle_filled,
                                size: Adapt.px(36),
                              )
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                );
              },
              itemCount: 3,
              viewportFraction: 0.8,
              scale: 0.8,
              duration: 300,
              autoplay: false,
            ),
    );
  }

  // @override
  // // TODO: implement wantKeepAlive
  // bool get wantKeepAlive => true;
}
